<?php

declare(strict_types=1);


namespace app\controller\mini;

use app\controller\admin\AdminBaseController;
use app\model\admin\ApplyMoneySet;
use app\model\admin\Areas;
use app\model\admin\MiniBalanceLog;
use app\Request;
use app\model\admin\SysMember;
use app\model\mini\Address;
use app\model\mini\BalanceLog;
use app\model\mini\MiniApply;
use app\model\mini\MiniUser;
use Exception;
use think\facade\Db;

class ApplyController extends AdminBaseController
{
    public function getApplyMoneyList(Request $request)
    {
        $list = ApplyMoneySet::where('status', 1)->order('sort', "desc")->select();
        return success($list);
    }
    public function getApplyLogList(Request $request)
    {
        $user_id = $request->user_id;
        $list = MiniApply::where('user_id', $user_id)->order('id', 'desc')->paginate([
            'list_rows' => $this->limit,
            'page' => $this->page,
            'var_page' => $this->pageVar
        ]);
        return successPage($list);
    }
    // 申请提现
    public function createApply(Request $request)
    {
        $params = $request->param();
        // $officialUser = request()->officialUser;
        // $user_id = $officialUser['user_id'];
        $user_id = $request->user_id;
        if (!isset($params['wx_num'])) {
            return error('请填写微信号');
        }
        if (!isset($params['apply_set_id'])) {
            return error('请选择提现金额');
        }
        // if ($params['money'] <= 0) {
        //     return error('提现失败');
        // }
        // 验证余额
        $user_info = MiniUser::find($user_id);
        if (!$user_info) {
            return error('提现失败，请联系管理员!');
        }
        $moneySet = ApplyMoneySet::find($params['apply_set_id']);
        if (!$moneySet) {
            return error('未配置提现金额');
        }
        if ($moneySet->status != 1) {
            return error('配置的提现金额已禁用');
        }
        $money = $moneySet->money;
        $balance = $user_info->balance;

        if ($balance < $money) {
            // 账户余额小于提现金额，不能提现
            return error("余额不足,当前账户余额:{$balance}!");
        }
        $rate_num = 0;
        // 手续费
        // $mp_config = MpConfig::where('sys_user_id', $user_info->create_by)->find();
        // if ($mp_config && $mp_config->apply_rate) {
        //     $rate_num = $mp_config->apply_rate;
        // }
        // 手续费
        $money_rate = 0;  //每笔提现手续费
        if ($rate_num) {
            $rate = $money * $rate_num / 100 + $money_rate;
        } else {
            $rate = $money_rate;
        }

        $params['user_id'] = $user_id;
        $params['status'] = 1;
        $params['rate'] = $rate;
        $params['sys_user_id'] = $user_info->create_by;

        // 实际到账金额
        $real_money = $money - $rate;
        if ($real_money < 0) {
            return error('提现异常，请联系管理员');
        }
        $params['real_money'] = $real_money;
        try {
            Db::transaction(function () use ($user_id, $params, $user_info, $money) {

                $order_sn = "TX" . date("YmdHis") . $user_id . rand(100, 999) . rand(100, 999) . rand(100, 999);
                $params['order_sn'] = $order_sn;
                $params['money'] = $money;
                $res = MiniApply::create($params);
                // 记录日志
                BalanceLog::create([
                    "user_id" => $user_id,
                    "type" => 2,
                    "profit_type" => 9,
                    "order_sn" => $order_sn,
                    "money" => $money,
                    "bf_money" => $user_info->balance,
                    "af_money" => round($user_info->balance - $money, 2),
                    // "sys_user_id" => $user_info->create_by,
                    "create_by" => $user_info->create_by,
                    "remark" => "用户发起提现",
                ]);
                Db::table('mini_user')->where('id', $user_id)->dec('balance', floatval($money))->update();
                // 通知管理员
                // $admin_user = MiniUser::where('create_by', $user_info->create_by)->where('is_admin', 1)->find();
                // if ($admin_user) {
                //     // 提现发起通知队列
                //     $queue_data =  [
                //         "sys_user_id" => $user_info->create_by,
                //         "mp_user_id" => $user_id,
                //         'tx_created_at' => date("Y-m-d H:i:s"),
                //         "tx_wx_num" => $params['wx_num'],
                //         "tx_money" => $money,
                //         "order_id" => null,
                //         "to_open_id" => $admin_user->openid,
                //         'type' => "apply_money_notify",
                //         "nickname" => $user_info->nickname,
                //         // "user_money" => round($user_info->balance - $money, 2)
                //     ];

                //     $queue_res = Queue::push(SendTemp::class, $queue_data);
                //     if ($queue_res) {
                //         storage_log('提现发起通知队列请求成功');
                //     } else {
                //         storage_log('提现发起通知队列请求失败');
                //     }
                // }
            });
        } catch (Exception $e) {
            return error("申请失败!" . $e->getMessage());
        }
        return success();
    }
}
